package com.vf.cloud.paas.app.master.api.scene.controller;


import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.vf.cloud.paas.app.master.common.domain.SceneShare;
import com.vf.cloud.paas.app.master.common.domain.User;
import com.vf.cloud.paas.common.datasource.annotation.Tx;
import com.vf.cloud.paas.common.util.R;
import com.vf.cloud.paas.common.util.UuidUtil;
import com.vf.cloud.paas.common.util.WebUtils;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/scene/share")
public class SceneShareController {

	@Tx
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<SceneShare> save(HttpServletRequest request, SceneShare sceneCamera) {
		try {
			User user = WebUtils.getCurUser(request);

			if (StrKit.notBlank(sceneCamera.getId())) {
				sceneCamera.setModifyUserId(user.getCreateUserId());
				if (sceneCamera.update()) {
					return R.ok(sceneCamera);
				} else {
					return R.failed();
				}
			} else {
				sceneCamera.setId(UuidUtil.getUUID());
				sceneCamera.setModifyUserId(user.getId());
				if (sceneCamera.save()) {
					return R.ok(sceneCamera);
				} else {
					return R.failed();
				}
			}
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public R<String> delete(@RequestBody SceneShare param) {
		if (param == null) {
			return R.failed("参数必填.");
		}
		if (SceneShare.dao.deleteById(param.getId())) {
			return R.ok();
		}
		return R.failed();
	}
}
